package com.fitness.front.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.front.convertor.RechargeConvertor;
import com.fitness.front.exception.BizException;
import com.fitness.front.mapper.RechargeMapper;
import com.fitness.front.modal.wallet.entity.Recharge;
import com.fitness.front.modal.wallet.params.RechargeVOQueryParams;
import com.fitness.front.modal.wallet.vo.RechargeVO;
import com.fitness.front.service.IRechargeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, Recharge> implements IRechargeService {

    private final RechargeConvertor rechargeConvertor;

    @Override
    public Page<RechargeVO> getPage(RechargeVOQueryParams queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<Recharge> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Recharge> queryWrapper = new QueryWrapper<>();
        Long memberId = queryParams.getMemberId();
        if(memberId == null){
            log.error("没有会员信息");
            throw new BizException("缺少会员id");
        }
        queryWrapper.eq("member_id", memberId);
        String type = queryParams.getType();
        if(!StrUtil.isBlank(type)){
            queryWrapper.eq("type", type);
        }
        // 查询数据
        Page<Recharge> coachBoPage = this.baseMapper.selectPage(page, queryWrapper);

        return rechargeConvertor.entity2Vo(coachBoPage);
    }
}
